package ipaintplugin.objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Oval extends Polygon {

	public static final String TAG_NAME = "oval";
	private Point center;
	private int width = 1;
	private int height = 1;

	public Oval() {

	}

	@Override
	public void fromXML(Node el) {
		center = new Point(Integer.parseInt(el.getAttributes().getNamedItem(
				"centerx").getNodeValue()), Integer.parseInt(el.getAttributes()
				.getNamedItem("centery").getNodeValue()));
		width = Integer.parseInt(el.getAttributes().getNamedItem("width")
				.getNodeValue());
		height = Integer.parseInt(el.getAttributes().getNamedItem("height")
				.getNodeValue());

		super.fromXML(el);
	}

	public Point getCenter() {
		return center;
	}

	public void setCenter(Point center) {
		this.center = center;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	@Override
	public void draw(GC canvas) {
		super.draw(canvas);
		
		if (fillColor != null && fillColor != getColor(SWT.TRANSPARENT))
			canvas.fillOval(center.getX() - (width / 2), center.getY()
					- (height / 2), width, height);
		
		canvas.drawOval(center.getX() - (width / 2), center.getY()
				- (height / 2), width, height);
		
	}

	@Override
	public void toXML(Document doc, Element root) {
		Element el = doc.createElement(TAG_NAME);
		super.toXML(doc, el);
		
		el.setAttribute("centerx", String.valueOf(center.getX()));
		el.setAttribute("centery", String.valueOf(center.getY()));
		el.setAttribute("width", String.valueOf(width));
		el.setAttribute("height", String.valueOf(height));

		root.appendChild(el);

		

	}

	@Override
	public String getTagName() {
		return TAG_NAME;
	}

	@Override
	public Object clone() {
		// TODO Auto-generated method stub
		return new Oval();
	}
}
